/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Date;
import java.util.List;
import javax.persistence.*;
import jpapractica.*;


public class PersonManager {
    EntityManagerFactory emf;
    EntityManager managerPerson;
    Query Query;
    
    public PersonManager(){
        emf= Persistence.createEntityManagerFactory("GRLUPU");
        managerPerson= emf.createEntityManager();
    }
    
    public Person create (int id, String name, String lastname, String picture,
                            String birthplace, int dia, int mes, int ano){
        
        Person persona = new Person(id, name, lastname, picture, birthplace,dia,mes,ano);
        
      
        managerPerson.getTransaction().begin();
        managerPerson.persist(persona);
        managerPerson.getTransaction().commit();
        
        return persona;
        
    }
    public void delete(int id){
        Person persona =managerPerson.find(Person.class, id);
        managerPerson.getTransaction().begin();
        managerPerson.remove(persona);
        managerPerson.getTransaction().commit();
        
    }
    public void update(int id, String name, String lastname, String picture,
                            String birthplace, int dia, int mes, int ano){
        Person persona =managerPerson.find(Person.class, id);
        managerPerson.getTransaction().begin();
        persona.setName(name);
        persona.setLastname(lastname);
        persona.setPicture(picture);
        persona.setBirthplace(birthplace);
        persona.setBirthdate(dia,mes,ano);
        managerPerson.getTransaction().commit();
        
    }    
    

    
    public List<Person> buscar(String palabra){
   
        
        Query busqueda =managerPerson.createQuery("SELECT p FROM Person p WHERE p.name='"+palabra+"'");
        List<Person> aux =busqueda.getResultList();
         return aux;
    }
    
    
    
    
    
    
}
